/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.trafficando.util;

import com.appspot.trafficando.domain.EventType;
import com.appspot.trafficando.domain.Constants;
import java.util.Date;
import javax.xml.namespace.QName;
import org.apache.abdera.Abdera;
import org.apache.abdera.ext.geo.Position;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Category;
import org.apache.abdera.model.DateTime;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Element;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
import org.apache.abdera.model.Person;
import org.apache.abdera.parser.Parser;
import org.apache.abdera.parser.stax.SerializableFOMEntry;
import org.apache.abdera.writer.WriterFactory;
import org.apache.axiom.om.OMContainer;
import org.apache.axiom.om.OMFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Domenico Maria Giffone
 */
public class AtomHelper {

    private static Factory abderaFactory = new Abdera().getFactory();
    private static final Logger LOG = LoggerFactory.getLogger(AtomHelper.class);

    ;
    public static final String DC_NS = "http://purl.org/dc/elements/1.1/";
    public static final QName QNAME_DC_TYPE = new QName(DC_NS, "type", "dc");
    public static final QName QNAME_DATE = new QName("pubDate");
    public static final QName QNAME_DC_DATE = new QName(DC_NS, "date");

    public static void addType(Entry entry, EventType type) {
        Element typeElement = entry.addExtension(QNAME_DC_TYPE);
        typeElement.setText(type.name());
    }

    public static Date getDate(Entry item) {
        DateTime dt = item.getExtension(QNAME_DC_DATE);
        return dt.getDate();

    }

    public static void copyEntry(Entry source, Entry destination) {
        destination.setTitle(source.getTitle());
        destination.setSummary(source.getSummary());
        destination.setPublished((source.getPublished() == null) ? AtomHelper.getDate(source) : source.getPublished());
        if (EventType.UNKNOWN.equals(getType(destination))) {
            addType(destination, getType(source));
        }
        for (Person person : source.getContributors()) {
            destination.addContributor(person);
        }
        Position[] positions = org.apache.abdera.ext.geo.GeoHelper.getPositions(source);
        for (Position position : positions) {
            org.apache.abdera.ext.geo.GeoHelper.addPosition(destination, position);
        }
        for (Category category : source.getCategories()) {
            destination.addCategory(category);
        }
    }

    public static EventType getType(Entry entry) {
        return (null == entry.getExtension(QNAME_DC_TYPE))
                ? EventType.UNKNOWN : (null == entry.getExtension(QNAME_DC_TYPE).getText() || "".equals(entry.getExtension(QNAME_DC_TYPE).getText().trim()))
                ? EventType.UNKNOWN : EventType.valueOf(entry.getExtension(QNAME_DC_TYPE).getText());
    }

    public static Feed getEmptyFeed() {
        Feed result = abderaFactory.newFeed();
        result.setTitle(Constants.FEED_TITLE);
        result.addLink(Constants.FEED_LINK);
        return result;
    }

    public static Entry getEmptyEntry() {
        //return abderaFactory.newEntry();
        return getSerializableEntry();
    }

    public static Parser getParser() {
        return abderaFactory.getAbdera().getParser();
    }

    public static WriterFactory getWriterFactory() {
        return abderaFactory.getAbdera().getWriterFactory();
    }

    public static Factory getFactory() {
        return abderaFactory;
    }

    public static Entry getSerializableEntry() {
        Document<Entry> doc = abderaFactory.newDocument();
        return new SerializableFOMEntry((OMContainer) doc, (OMFactory) abderaFactory);
    }
}
